package com.lqq.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lqq.commonutils.JwtUtils;
import com.lqq.commonutils.R;
import com.lqq.eduorder.entity.Order;
import com.lqq.eduorder.entity.OrderQuery;
import com.lqq.eduorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 677
 * @since 2021-07-12
 */
@RestController
@RequestMapping("/eduorder/order")
//@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    //1.生成订单
    @PostMapping("/createOrder/{courseId}")
    public R saveOrder(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        //根据request获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) {
            return R.error().code(20001).message("请先登录!");
        }
        //创建订单，返回订单号
        String orderNo = orderService.createOrder(courseId, memberId);
        return R.ok().data("orderId", orderNo);
    }

    //2.根据订单id查询订单信息
    @GetMapping("/getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable("orderId") String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);
        Order orderInfo = orderService.getOne(wrapper);
        return R.ok().data("item", orderInfo);
    }

    //3.根据课程id和用户id查询订单表的订单状态
    @GetMapping("/isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable("courseId") String courseId,
                               @PathVariable("memberId") String memberId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId)
                .eq("member_id", memberId)
                .eq("status", 1);//支付状态，1代表已支付
        int count = orderService.count(wrapper);
        if (count > 0) {
            return true;//支付
        } else {
            return false;//未支付
        }
    }

    //条件分页查询订单信息
    @PostMapping("/list/{current}/{limit}")
    public R list(@PathVariable("current") Long current,
                  @PathVariable("limit") Long limit,
                  @RequestBody(required = false) OrderQuery orderQuery) {
        //分页
        Page<Order> page = new Page<>(current, limit);

        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        //获取查询条件
        String orderNo = orderQuery.getOrderNo();
        String mobile = orderQuery.getMobile();
        Integer status = orderQuery.getStatus();

        if (!StringUtils.isEmpty(orderNo)) {//加入条件1
            wrapper.eq("order_no", orderNo);
        }

        if (!StringUtils.isEmpty(mobile)) {
            wrapper.eq("mobile", mobile);
        }

        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }

        orderService.page(page, wrapper);

        Map map = new HashMap();
        map.put("total", page.getTotal());
        map.put("items", page.getRecords());

        return R.ok().data("list", map);
    }

    //删除订单
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        boolean remove = orderService.removeById(id);
        if (remove) {
            return R.ok().message("删除订单成功");
        }
        else {
            return R.ok().message("删除订单失败");
        }
    }

    //编辑订单
    @PostMapping("/updateOrder")
    public R updateOrder(@RequestBody Order order) {

        boolean update = orderService.updateById(order);
        if (update) {
            return R.ok().message("修改订单信息成功");
        }else {
            return R.error().message("修改订单信息失败");
        }
    }

}